package com.yaojiaxun.logisticsPlatformSystem.controller;

import com.yaojiaxun.logisticsPlatformSystem.dto.MaterialInfoDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.MaterialInfo;
import com.yaojiaxun.logisticsPlatformSystem.entity.MaterialSearch;
import com.yaojiaxun.logisticsPlatformSystem.service.MaterialInfoService;
import com.yaojiaxun.logisticsPlatformSystem.util.ResultVOUtil;
import com.yaojiaxun.logisticsPlatformSystem.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @Auther: yjx_j
 * @Date: 2019/1/21 10:42
 * @Description:
 */
@Controller
public class MaterialInfoController {

    @Autowired
    MaterialInfoService materialInfoService;

    @RequestMapping("/material_manage")
    public String getMaterialIndex(@RequestParam(defaultValue = "1") Integer page, Map<String, Object> map){
        map.put("pageDTO", materialInfoService.findPage(page));
        return "system/material/material_manage";
    }

    @RequestMapping("/material_detail")
    public String getDetail(@RequestParam("materialId") Long materialId, @RequestParam("page") Integer page, Map<String, Object> map){
        map.put("page", page);
        map.put("materialInfo", materialInfoService.findMaterialById(materialId));
        return "system/material/material_detail";
    }

    @RequestMapping("/material_update_after")
    public String updateMaterialAfter(@RequestParam("page") Integer page, MaterialInfo materialInfo){
        materialInfoService.updateMaterialByMaterialId(materialInfo);
        return "redirect:/material_manage?page="+page;
    }

    @RequestMapping("/materialAddBefore")
    public String addMaterialBefore(){
        return "system/material/material_add";
    }

    @RequestMapping("/material_add_after")
    public String addMaterialAfter(MaterialInfo materialInfo){
        materialInfoService.insertMaterialInfo(materialInfo);
        return "redirect:/material_manage";
    }

    @RequestMapping("/materialDelete")
    public String deleteMaterialById(@RequestParam("materialId") Long materialId, @RequestParam("page") Integer page){
        materialInfoService.deleteMaterial(materialId);
        return "redirect:/material_manage?page="+page;
    }

    //按供应商/客户或物资名称查找物资信息
    @RequestMapping("/findMaterialByCompany")
    public String findMatByCompanyOrMatName(@RequestParam("comOrName") String comOrName, Map<String, Object> map){
        map.put("results", materialInfoService.findMatByCompanyOrMatName(comOrName));
        return "system/material/material_search_results";
    }

    //按公司名称查找物资信息Json
    @ResponseBody
    @RequestMapping("/findMaterialByCompanyJson")
    public ResultVO findMatByCompany(@RequestParam("companyName") String companyName){
        return ResultVOUtil.success(materialInfoService.findMaterialByCompany(companyName));
    }

    //按出发地和到达地搜索货源
    @RequestMapping("/material_list_search")
    public String findMaterialByRAndD(@RequestParam(defaultValue = "1") Integer page, @RequestParam("rProvince") String rProvince, @RequestParam("rCity") String rCity, @RequestParam("rCountry") String rCountry,
                                      @RequestParam("dProvince") String dProvince, @RequestParam("dCity") String dCity, @RequestParam("dCountry") String dCountry, Map<String,Object> map, MaterialSearch materialSearch){
        materialSearch.setRProvince(rProvince);
        materialSearch.setRCity(rCity);
        materialSearch.setRCountry(rCountry);
        materialSearch.setDProvince(dProvince);
        materialSearch.setDCity(dCity);
        materialSearch.setDCountry(dCountry);
        PageDTO<MaterialInfoDTO> pageDTO = materialInfoService.findPage(page, materialSearch);
        map.put("rProvince", rProvince);
        map.put("rCity", rCity);
        map.put("rCountry", rCountry);
        map.put("dProvince", dProvince);
        map.put("dCity", dCity);
        map.put("dCountry", dCountry);
        map.put("pageDTO", pageDTO);
        return "front/material/material_list_search";
    }
}
